﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblAct_ContactPersonDAL:ItblAct_ContactPerson
    {
        HKEA_CMSEntities model;
        tblAct_ContactPerson person;

        public tblAct_ContactPersonDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblAct_ContactPersonDAL GetInstance()
        {
            return new tblAct_ContactPersonDAL();
        }

        public int InsertENT(tblAct_ContactPerson ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblAct_ContactPerson.AddObject(ent);
            return model.SaveChanges();
        }

        public List<tblAct_ContactPerson> GetListByENT(tblAct_ContactPerson ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblAct_ContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblAct_ContactPerson> GetListByENT(tblAct_ContactPerson ent)
        {
            var result = model.tblAct_ContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblAct_ContactPerson ent)
        {
            var result = model.tblAct_ContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a => a.ContactPersonUID == ent.ContactPersonUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblAct_ContactPerson ent)
        {
            person = model.tblAct_ContactPerson.Where(a=>a.ContactPersonUID==ent.ContactPersonUID).FirstOrDefault();
            if (person != null)
            {
                person.ActiviteUID = ent.ActiviteUID;
                person.Deleted = ent.Deleted;
                person.LastModifyBy = ent.LastModifyBy;
                person.LastModifyDate = DateTime.Now;
                person.ActContactUID = ent.ActContactUID;
                person.ActualNoOfAttend = ent.ActualNoOfAttend;
                person.Attendee_StatusUID = ent.Attendee_StatusUID;
                person.CustomerUID = ent.CustomerUID;
                person.NoOfAttend = ent.NoOfAttend;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblAct_ContactPerson ent)
        {
            var temp = model.tblAct_ContactPerson.Where(a => a.ContactPersonUID > 0);
            if (ent.ContactPersonUID > 0)
                temp = temp.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblAct_ContactPerson.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
